<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://www.ufida.com/lfw" prefix="lfw" %>
<%@ taglib uri="http://www.ufida.com/uif" prefix="uif" %> 
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<html>
	<head>
		<lfw:base/>
		<lfw:import/>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<script type='text/javascript' src='${ROOT_PATH}/html/nodes/jkbxscript/WebbxJsUtil.js'></script>
		<script>
			function afterPageInit() 
			{
		 		var ds = getDataset('webbx_main_query_ds');
		 		ds.setRowSelected(0);
			}
		</script>
	</head>
	<body bottommargin="0" topmargin="0" leftmargin="0" rightmargin="0" scroll=no onload="pageBodyScript()" style="background-color:transparent">
		<lfw:pageModel className="nc.lfw.finance.quickdesk.WebBxQuickDeskQueryPageModel">
			<lfw:layout type="border" border="0">
				<lfw:layoutPanel position="center">
					<lfw:form id="webbx_main_query_form"></lfw:form>
				</lfw:layoutPanel>
				<lfw:layoutPanel align="center" position="bottom" height="34">
					<lfw:button id="query_confirm"></lfw:button>
				</lfw:layoutPanel>
			</lfw:layout>
		</lfw:pageModel>
	</body>
</html>	
<script language="javascript">
	
	function doQuickQuery()
	{  
		var sql = getQuickQuerySql();
		$query(sql);
	}
	
	function $query(sql)
	{
		var queryParamArray = '';
		log("quick query condition sql: " + sql);
		var dsId = '<%=request.getParameter("dsId")%>';
		var targetDs = parent.getDataset(dsId);
		targetDs.reqParameterMap.clear();
		targetDs.addReqParameter('WEBBX_MAIN_QUERY', sql);
		targetDs.addReqParameter('WEBBX_MAIN_ADVANCED_QUERY_FLAG','Y');
		targetDs.addReqParameter(IDatasetConstant.FROM_QUERY_TEMPLATE, "false");

		var ds = getDataset("webbx_main_query_ds");
		var row = ds.getSelectedRow();
		var done = row.getCellValue(ds.nameToIndex('done'));
		var notdone = row.getCellValue(ds.nameToIndex('notdone'));
		targetDs.addReqParameter("WEBBX_MAIN_QUERY_DONE", done);
		targetDs.addReqParameter("WEBBX_MAIN_QUERY_NOTDONE",notdone);
		
		var args = [targetDs];
		if(dsId == "myDjDs")
			parent.showLoadingDataDiv(parent.getComponent("myDjGrid").outerDiv);
		else if(dsId == "myJobDs")
			parent.showLoadingDataDiv(parent.getComponent("myJobGrid").outerDiv);
		
		var loader = new DatasetLoader(targetDs);
		loader.setKeyValue(Dataset.MASTER_KEY);
		loader.setReturnFunc($qryReturnFunc);
		loader.setReturnArgs(args);
		loader.load(parent);
	}
	
	function getQuickQuerySql()
	{
		var ds = getDataset("webbx_main_query_ds");
		var row = ds.getSelectedRow();
		var sql = new StringBuffer(); 
		if(row != null){
			var djlx = row.getCellValue(ds.nameToIndex('djlx_combo'));
			if(!isEmpty(djlx)){
				sql.append(" a.djlxbm=");
				sql.append("'" + djlx + "'");
			}
				
			var spzt = row.getCellValue(ds.nameToIndex('spzt_combo'));
			if(!isEmpty(spzt)){
				if(!sql.isEmpty())
					sql.append(' and ');
				sql.append(" a.spzt=");
				if(spzt == '-10000')
					sql.append(" a.djzt=0 ");
				else
					sql.append("'" + spzt + "'");
			}
			var startdate = row.getCellValue(ds.nameToIndex('startdate'));
			if(isEmpty(startdate )) 
				startdate =  '1970-01-01';
			var enddate = row.getCellValue(ds.nameToIndex('enddate'));
			if(isEmpty(enddate))
				enddate = '2099-01-01';
			if(!sql.isEmpty())
				sql.append(' and ');
			sql.append(" (a.djrq >= '");
			sql.append(startdate);
			sql.append("' and a.djrq <='");
			sql.append(enddate + "')");
				
			var pf_startdate = row.getCellValue(ds.nameToIndex('pf_startdate'));
			var pf_enddate = row.getCellValue(ds.nameToIndex('pf_enddate'));
			var tmp = '';
			if(!isEmpty(pf_startdate)){
				tmp += " a.shrq >= '";
				tmp += pf_startdate + "'";
			}
			if(!isEmpty(pf_enddate)){
				if(tmp != '')
					tmp += " and";
				tmp += " a.shrq <='" ;
				tmp += pf_enddate + "'";
			}
			if(tmp != ''){
				sql.append(" AND (");
				sql.append(tmp);
				sql.append(" OR a.shrq IS NULL )");
			}
			/*
			var done = row.getCellValue(ds.nameToIndex('done'));
			var notdone = row.getCellValue(ds.nameToIndex('notdone'));
			if(!isEmpty(done) && done == 'Y'){
				sql.append(" AND ((a.payflag = 3 OR a.payflag = 99) AND a.spzt = '1')");
			}
			if(!isEmpty(notdone) && notdone == 'Y'){
				sql.append(" AND ((a.payflag <> 3 AND a.payflag <> 99 ) or a.payflag is null)");
			}
			*/
		}
		var sqlstr = sql.toString();
		if(sql.isEmpty()) 
			sqlstr = '1=1';
		return sqlstr;
	}
	
	function isEmpty(value){
		return value == null || value == '' || value == "-11111";
	}
	
	function $qryReturnFunc(args, isSuccess) {
		if(isSuccess){
			var ds = args[0];
			if(ds.getRowCount() > 0 && ds.getSelectedIndex() == -1)
				ds.setRowSelected(0);
			parent.resetSumPrecision(ds);
		}
		parent.hideDialog("quickquery_dialog", false);
	}
	
	function getMainFrame() {
		return parent.mainFrame;
	}
</script>
